<template>
  <div class="app-container pagination">
    <el-card>
      <div class="header">
        <div>
          <!--<el-select v-model="search['status']" placeholder="机场状态" class="select" clearable @change="()=>pagination['load']()">
            <el-option value="开启" label="开启"/>
            <el-option value="禁用" label="禁用"/>
          </el-select>
          <el-select v-model="search['status2']" placeholder="举牌服务" class="select" clearable @change="()=>pagination['load']()">
            <el-option value="支持" label="支持"/>
            <el-option value="不支持" label="不支持"/>
          </el-select>-->
        </div>
        <div>
          <router-link :to="`./add?carType=${search['carType']}`">
            <el-button type="primary" icon="el-icon-plus" class="margin-right-10">添加车辆</el-button>
          </router-link>
        </div>
      </div>
      <ws-pagination v-model="pagination">
        <el-table-column prop="id" label="ID" width="90" header-align="center" align="center"/>
        <el-table-column label="车型缩略图" width="210" header-align="center" align="center">
          <template slot-scope="scope">
            <ws-img :src="scope.row['iconUrl']" width="66px" height="66px"/>
          </template>
        </el-table-column>
        <el-table-column prop="appearance" label="颜色" width="140" header-align="center" align="center"/>
        <el-table-column prop="name" label="品牌" min-width="150" header-align="center" align="center"/>
        <el-table-column prop="modelNumber" label="型号" min-width="160" header-align="center" align="center"/>
        <el-table-column prop="plateNumber" label="车牌号" min-width="160" header-align="center" align="center"/>
        <el-table-column prop="carTypeText" label="类型" width="140" header-align="center" align="center"/>
        <el-table-column prop="typeText" label="动力类型" width="120" header-align="center" align="center"/>
        <el-table-column prop="seat" label="座位数" width="120" header-align="center" align="center"/>
        <el-table-column prop="remark" label="备注" min-width="160" header-align="center" align="center"/>
        <el-table-column prop="status" label="状态" width="120" header-align="center" align="center" fixed="right">
          <template slot-scope="scope">
            <div @click="changeStatus(scope.row)">
              <el-tag v-if="scope.row.status===1" type="success">已启用</el-tag>
              <el-tag v-else type="info">已禁用</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="160" header-align="center" align="center" fixed="right">
          <template slot-scope="scope">
            <!--详情-->
            <!--<router-link :to="`./details?id=${scope['row']['id']}`">
              <el-button size="small" type="primary" icon="el-icon-search" circle/>
            </router-link>-->
            <!--编辑-->
            <router-link :to="`./edit?id=${scope['row']['id']}`">
              <el-button size="small" type="primary" icon="el-icon-edit" circle/>
            </router-link>
            <!--删除-->
            <el-button size="small" type="danger" icon="el-icon-delete" circle @click="deleteRow(scope['row'])"/>
          </template>
        </el-table-column>
      </ws-pagination>
    </el-card>
  </div>
</template>

<script>
  export default {
    name: 'InvoiceList',
    components: {},
    data() {
      return {
        // 查询条件
        search: {
          // 搜索内容
          searchValue: '',
          // 状态
          status: '',
          // 车辆类型id
          carType: ''
        },
        pagination: {}
      }
    },
    watch: {},
    created() {
      this.search['carType'] = this.$router.currentRoute.query['carType'] || '';
      // 初始化分页器
      this.initPagination();
    },
    methods: {
      // 初始化分页器
      initPagination() {
        // 创建分页器
        this.pagination = new this.$pagination(async (...args) => {
          // 加载分页列表
          const result = await this.$service.applicationGetCarList({
            ...args[0],
            name: this.search.name,
            status: this.search.status,
            carType: this.search.carType
          });
          if (result.code !== 0) {
            return this.$message.error(result.msg || '系统错误');
          }
          return result;
        });
        // 加载分页数据
        this.pagination.load();
      },
      // 删除指定行
      deleteRow({id}) {
        this.$confirm('此操作将删除该车辆, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          callback: async (confirm) => {
            if (confirm === 'confirm') {
              // 创建一级分类
              const result = await this.$service.applicationDeleteCar({id: id});
              if (result.code === 0) {
                this.$message.success(`删除成功~`);
                // 加载分页数据
                this.pagination.load();
              } else {
                this.$message.error(result.msg);
              }
            }
          }
        });
      },
      // 切换状态
      changeStatus({id, status}) {
        this.$confirm(`你确定要${status === 1 ? '禁用' : '启用'}这个车辆吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          callback: async (confirm) => {
            if (confirm === 'confirm') {
              const result = await this.$service.applicationChangeCarStatus({
                id: id,
                status: status === 1 ? 2 : 1
              });
              if (result.code === 0) {
                this.$message.success(`${status === 1 ? '禁用' : '启用'}成功~`);
                // 加载分页数据
                this.pagination.load();
              } else {
                this.$message.error(result.msg);
              }
            }
          }
        });
      }
    }
  }
</script>

<style lang="scss" type="text/scss" scoped>
  @import '../../../styles/index.scss';
  .pagination {
    .header {
      @include flex-row(center, space-between);
      div{
        @include flex-row(center, flex-start);
        .search-input {
          width: 240px;
          margin-right: 5px;
        }
        .el-select{
          width: 160px;
          margin-right: 20px;
        }
      }
    }
  }
</style>
